﻿using KinonekoSoftware.CodariaDB.Documents.DataParts.Modules.RVE;
using KinonekoSoftware.CodariaDB.Documents.Theories.Elements;

namespace KinonekoSoftware.Studio.Models.Modules.RVE
{
    public sealed class RVEElementRibbonPresenter : RVE_Presenter<RVE_ElementRibbon>
    {
        private  Element _element;

        public RVEElementRibbonPresenter()
        {
            var collection = Platform.GetEngine<TheoryEngine_Element>().Collection;
            _element = collection.FirstOrDefault();

            AssociatedObject = new RVE_ElementRibbon
            {
                Id        = ID.Get(),
                ElementID = _element?.Id,
            };
        }

        public RVEElementRibbonPresenter(RVE_ElementRibbon rve_element, Element element)
        {
            _element         = element ?? throw new ArgumentNullException(nameof(element));
            AssociatedObject = rve_element;
            Title            = rve_element.Title;
            Description      = rve_element.Description;
            RaiseUpdated(nameof(Color));
            RaiseUpdated(nameof(Value));
            RaiseUpdated(nameof(Icon));
            RaiseUpdated(nameof(Foreground));
        }
        
        public override string GetTitle() => string.IsNullOrEmpty(Title) ? CodariaSR.Module_RVE_Element : Title;
        public override bool IsMultipleVariants() => false;
        public override IEnumerable<Variant> GetVariants(string documentID) => null;
        public override Variant GetVariant(string documentID)
        {
            return new StringVariant
            {
                Id       = documentID,
                Metadata = KnownMetadatas.Element,
                Value    = Element?.Name,
                Type     = VariantType.String,
            };
        }
        
        
        public Element Element
        {
            get => _element;
            set
            {
                if(value is null)
                {
                    return;
                }
                _element = value;
                AssociatedObject.ElementID = value.Id;
                RaiseUpdated();
                RaiseUpdated(nameof(Color));
                RaiseUpdated(nameof(Value));
                RaiseUpdated(nameof(Icon));
                RaiseUpdated(nameof(Foreground));
            }
        }
        
        
        public          string          Color    => _element?.Background ?? "#007ACC";
        public          string          Value    => _element?.Name ?? "缺失稀有度";
        public          string          Icon     => _element?.Icon;
        public string Foreground => _element?.Foreground;
    }
}